{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "58aa3d75-5a9b-43d8-8bea-f391319e5710",
   "metadata": {},
   "source": [
    "Chapter 03\n",
    "\n",
    "# 矩阵乘法第三视角\n",
    "《线性代数》 | 鸢尾花书：数学不难"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e280b673-1a06-4913-ba7b-2f39f60cd7e1",
   "metadata": {},
   "source": [
    "这段代码的核心任务是执行矩阵运算，特别是矩阵转置和矩阵乘法。我们详细分析各个步骤的数学意义，并给出相应的公式。\n",
    "\n",
    "### **1. 初始化与矩阵定义**\n",
    "代码首先使用 `numpy` 定义了一个 $2 \\times 3$ 矩阵 $A$：\n",
    "$$\n",
    "A = \\begin{bmatrix} 1 & 2 & 3 \\\\ 4 & 5 & 6 \\end{bmatrix}\n",
    "$$\n",
    "然后计算其转置 $B$：\n",
    "$$\n",
    "B = A^T = \\begin{bmatrix} 1 & 4 \\\\ 2 & 5 \\\\ 3 & 6 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "### **2. 计算矩阵 $C = A B$**\n",
    "矩阵 $C$ 的计算过程如下：\n",
    "$$\n",
    "C = A B = \\begin{bmatrix} 1 & 2 & 3 \\\\ 4 & 5 & 6 \\end{bmatrix}\n",
    "\\begin{bmatrix} 1 & 4 \\\\ 2 & 5 \\\\ 3 & 6 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "逐列计算 $C$ 的每一列：\n",
    "- 第一列：\n",
    "  $$\n",
    "  c_1 = A \\cdot B_{[:,1]} = A \\cdot \\begin{bmatrix} 1 \\\\ 2 \\\\ 3 \\end{bmatrix}\n",
    " $$\n",
    "  计算得：\n",
    "  $$\n",
    "  c_1 = \\begin{bmatrix} 1 \\cdot 1 + 2 \\cdot 2 + 3 \\cdot 3 \\\\ 4 \\cdot 1 + 5 \\cdot 2 + 6 \\cdot 3 \\end{bmatrix}\n",
    "  = \\begin{bmatrix} 14 \\\\ 32 \\end{bmatrix}\n",
    " $$\n",
    "\n",
    "- 第二列：\n",
    "  $$\n",
    "  c_2 = A \\cdot B_{[:,2]} = A \\cdot \\begin{bmatrix} 4 \\\\ 5 \\\\ 6 \\end{bmatrix}\n",
    " $$\n",
    "  计算得：\n",
    "  $$\n",
    "  c_2 = \\begin{bmatrix} 1 \\cdot 4 + 2 \\cdot 5 + 3 \\cdot 6 \\\\ 4 \\cdot 4 + 5 \\cdot 5 + 6 \\cdot 6 \\end{bmatrix}\n",
    "  = \\begin{bmatrix} 32 \\\\ 77 \\end{bmatrix}\n",
    " $$\n",
    "\n",
    "拼接 $c_1$ 和 $c_2$，得到完整的矩阵 $C$：\n",
    "$$\n",
    "C = \\begin{bmatrix} 14 & 32 \\\\ 32 & 77 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "### **3. 计算矩阵 $D = B A$**\n",
    "类似地，计算 $D = B A$：\n",
    "$$\n",
    "D = B A = \\begin{bmatrix} 1 & 4 \\\\ 2 & 5 \\\\ 3 & 6 \\end{bmatrix}\n",
    "\\begin{bmatrix} 1 & 2 & 3 \\\\ 4 & 5 & 6 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "逐列计算 $D$ 的每一列：\n",
    "- 第一列：\n",
    "  $$\n",
    "  d_1 = B \\cdot A_{[:,1]} = B \\cdot \\begin{bmatrix} 1 \\\\ 4 \\end{bmatrix}\n",
    " $$\n",
    "  计算得：\n",
    "  $$\n",
    "  d_1 = \\begin{bmatrix} 1 \\cdot 1 + 4 \\cdot 4 \\\\ 2 \\cdot 1 + 5 \\cdot 4 \\\\ 3 \\cdot 1 + 6 \\cdot 4 \\end{bmatrix}\n",
    "  = \\begin{bmatrix} 17 \\\\ 22 \\\\ 27 \\end{bmatrix}\n",
    " $$\n",
    "\n",
    "- 第二列：\n",
    "  $$\n",
    "  d_2 = B \\cdot A_{[:,2]} = B \\cdot \\begin{bmatrix} 2 \\\\ 5 \\end{bmatrix}\n",
    " $$\n",
    "  计算得：\n",
    "  $$\n",
    "  d_2 = \\begin{bmatrix} 1 \\cdot 2 + 4 \\cdot 5 \\\\ 2 \\cdot 2 + 5 \\cdot 5 \\\\ 3 \\cdot 2 + 6 \\cdot 5 \\end{bmatrix}\n",
    "  = \\begin{bmatrix} 22 \\\\ 29 \\\\ 36 \\end{bmatrix}\n",
    " $$\n",
    "\n",
    "- 第三列：\n",
    "  $$\n",
    "  d_3 = B \\cdot A_{[:,3]} = B \\cdot \\begin{bmatrix} 3 \\\\ 6 \\end{bmatrix}\n",
    " $$\n",
    "  计算得：\n",
    "  $$\n",
    "  d_3 = \\begin{bmatrix} 1 \\cdot 3 + 4 \\cdot 6 \\\\ 2 \\cdot 3 + 5 \\cdot 6 \\\\ 3 \\cdot 3 + 6 \\cdot 6 \\end{bmatrix}\n",
    "  = \\begin{bmatrix} 27 \\\\ 36 \\\\ 45 \\end{bmatrix}\n",
    " $$\n",
    "\n",
    "拼接 $d_1, d_2, d_3$ 得：\n",
    "$$\n",
    "D = \\begin{bmatrix} 17 & 22 & 27 \\\\ 22 & 29 & 36 \\\\ 27 & 36 & 45 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "### **4. 结论**\n",
    "- $C = A A^T$ 是 $2 \\times 2$ 的对称矩阵，它表示矩阵 $A$ 的行向量之间的内积。\n",
    "- $D = A^T A$ 是 $3 \\times 3$ 的对称矩阵，它表示矩阵 $A$ 的列向量之间的内积。\n",
    "\n",
    "这两个矩阵分别出现在许多数学领域，例如：\n",
    "- $A A^T$ 出现在协方差矩阵计算中，用于衡量不同样本（行）之间的关系。\n",
    "- $A^T A$ 出现在主成分分析（PCA）中，用于衡量不同特征（列）之间的关系。\n",
    "\n",
    "整个代码实际上是构建 $A A^T$ 和 $A^T A$，并详细拆解了其计算过程。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "154068f3-d35a-48f9-a120-e6c5c276c659",
   "metadata": {},
   "source": [
    "## 初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "06bbbb2f-a859-41e4-8073-f154fe95ee4e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ba209c7-e728-44b6-b6ac-a04f59f37e37",
   "metadata": {},
   "source": [
    "## 定义矩阵 A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "99542863-0c1d-4fac-a6ea-1441d2085d97",
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.array([[1, 2, 3], \n",
    "              [4, 5, 6]])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e0bf4ae-7bf6-4963-b49f-e18db500d223",
   "metadata": {},
   "source": [
    "## 计算矩阵 B，即 A 的转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "30eff698-6fec-4b5d-ab1c-cd5e02b5e0e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "B = A.T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e354c300-c5aa-48c1-8624-637c1da961fc",
   "metadata": {},
   "source": [
    "## 计算矩阵乘法 C = A @ B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c444762e-ef35-4a4f-83ad-a217be49be42",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[14, 32],\n",
       "       [32, 77]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C = A @ B\n",
    "C"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c29675d-e59a-4874-bf56-b164496d14a4",
   "metadata": {},
   "source": [
    "## 第一列 $c_1$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "4d54dc5d-5b59-4396-ae7f-b9a7e1482d03",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "5e241bcc-48c4-499c-840d-4263605aea08",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [2],\n",
       "       [3]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B[:,[0]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "c5bdb5d4-4085-4542-9c21-13d2375f769f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[14],\n",
       "       [32]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c_1 = A @ B[:,[0]]\n",
    "c_1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7bd490b-ccc1-4763-bd45-7809f96d493a",
   "metadata": {},
   "source": [
    "## 第二列 $c_2$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "78b6b13a-f21b-4ac9-b3d4-0decdd035c65",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "b664af24-2577-4d08-9f4a-83df68883ea0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4],\n",
       "       [5],\n",
       "       [6]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B[:,[1]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "ddb2740c-f23d-46e4-ad6d-d2b1a102cb32",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[32],\n",
       "       [77]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c_2 = A @ B[:,[1]]\n",
    "c_2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b99c40e-97a6-4514-92a1-2e50efb0f56b",
   "metadata": {},
   "source": [
    "## 左右排列得到C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "dc1b7a7f-525b-4d22-975d-6e13afa71589",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[14, 32],\n",
       "       [32, 77]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.block([c_1, c_2])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "72f74467-3c52-44c9-bcdf-0e37cf5d3e10",
   "metadata": {},
   "source": [
    "## 计算B @ A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "b8939b70-2dda-4933-8111-8d57a92495da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[17, 22, 27],\n",
       "       [22, 29, 36],\n",
       "       [27, 36, 45]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "D = B @ A\n",
    "D"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "239a15a6-1422-47b7-ba4a-8f5d9c2c6cc8",
   "metadata": {},
   "source": [
    "## 第一列 $d_1$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "b170038d-bab9-46bb-9cb9-3ffe8fabf48c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 4],\n",
       "       [2, 5],\n",
       "       [3, 6]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "770211de-9caa-4abe-816d-85e5ee630736",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [4]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A[:,[0]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "4437cb27-8643-4285-9a35-5a35167c0e77",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[17],\n",
       "       [22],\n",
       "       [27]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d_1 = B @ A[:,[0]]\n",
    "d_1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89a142af-cca0-4e84-a56f-d0ff2d9b1461",
   "metadata": {},
   "source": [
    "## 第一列 $d_2$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "d395a44b-71b5-49ae-8ecc-4c5af4fda855",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 4],\n",
       "       [2, 5],\n",
       "       [3, 6]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "9e95da23-7eb8-4281-9690-7f20a1452bda",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2],\n",
       "       [5]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A[:,[1]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "44f953bb-fb61-4b18-9fd1-f8d7b08164c6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[22],\n",
       "       [29],\n",
       "       [36]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d_2 = B @ A[:,[1]]\n",
    "d_2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6513bce0-9d63-41a8-940a-ad5cd12b5463",
   "metadata": {},
   "source": [
    "## 第三列 $d_3$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "28f23b7c-6e78-4e3b-9e22-eb6639616d01",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 4],\n",
       "       [2, 5],\n",
       "       [3, 6]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "3c31afa2-eaae-43d1-8722-6a8fc0be5e8d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3],\n",
       "       [6]])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A[:,[2]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "7225060b-83aa-462d-b5a1-899c64541543",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[27],\n",
       "       [36],\n",
       "       [45]])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d_3 = B @ A[:,[2]]\n",
    "d_3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bff02253-44ef-46cd-ba41-b0ca762357f6",
   "metadata": {},
   "source": [
    "## 左右排列得到D"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "6af4530c-883e-4e89-a7d2-741d76943516",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[17, 22, 27],\n",
       "       [22, 29, 36],\n",
       "       [27, 36, 45]])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.block([d_1, d_2, d_3])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbd9fbf1-4951-4580-bc1b-a46b6c69986d",
   "metadata": {},
   "source": [
    "作者\t**生姜DrGinger**  \n",
    "脚本\t**生姜DrGinger**  \n",
    "视频\t**崔崔CuiCui**  \n",
    "开源资源\t[**GitHub**](https://github.com/Visualize-ML)  \n",
    "平台\t[**油管**](https://www.youtube.com/@DrGinger_Jiang)\t\t\n",
    "\t\t[**iris小课堂**](https://space.bilibili.com/3546865719052873)\t\t\n",
    "\t\t[**生姜DrGinger**](https://space.bilibili.com/513194466)  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
